﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_F5Part : Form
    {
        private DataSet ds;
        private DataViewManager dsView;
        public string partno;

        public frm_Edit_F5Part()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.ds = new DataSet("Part");
            this.partno = null;
            LoadPartList();
        }

        //////////////////////////////////////////
        /// Load Data
        ///////////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.partno))
            {
                ret = false;
            }
            else
            {
                if (this.partNo.FindString(this.partno) >= 0)
                {
                    this.partNo.SelectedIndex = this.partNo.FindString(this.partno);
                }
                if (this.partNo.SelectedIndex < 0)
                {
                    ret = false;
                }
                else
                {
                    try
                    {
                        myConnection.Open();
                        SqlCommand sql_cmd = myConnection.CreateCommand();
                        sql_cmd.CommandText = "SELECT F5Product, F5Level, F5Name, AssGroup, AssRemark,"
                            + " AssSerial, AssSTDTime FROM [222_Part]"
                            + " WHERE PartNo = '" + this.partno + "'";
                        SqlDataReader dataReader = sql_cmd.ExecuteReader();
                        if (dataReader.Read())
                        {
                            this.productText.Text = dataReader["F5Product"].ToString().Trim();
                            this.levelText.Text = dataReader["F5Level"].ToString().Trim();
                            this.nameText.Text = dataReader["F5Name"].ToString().Trim();
                            this.groupText.Text = dataReader["AssGroup"].ToString().Trim();
                            this.serialText.Text = dataReader["AssSerial"].ToString().Trim();
                            this.stdTime.Text = dataReader["AssSTDTime"].ToString().Trim();
                            this.noteText.Text = dataReader["AssRemark"].ToString().Trim();
                        }
                        else
                        {
                            ret = false;
                        }
                        dataReader.Close();
                        sql_cmd.Dispose();
                    }
                    catch
                    {
                        ret = false;
                    }
                    finally
                    {
                        myConnection.Close();
                    }
                }
            }
            return ret;
        }
        
        /// <summary>
        /// Load Part List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadPartList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [PartNo], [PartEnglishName]"
                    + " FROM [222_Part]"
                    + " WHERE F5 = 1";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "Part");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.partNo.DataSource = dsView;
                this.partNo.DisplayMember = "Part.PartNo";
                this.partNo.ValueMember = "Part.PartNo";
                this.partNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.partName.DataBindings.Add("Text", dsView, "Part.PartEnglishName");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }        
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;
            string part = null;
            string product = null;
            int level = 0;
            string name = null;
            string group = null;
            string serial = null;
            float stdtime = 0;
            string remark = null;

            if (this.partNo.SelectedIndex < 0)
            {
                err = "Please select Part to add.";
            }
            else
            {
                part = this.partNo.SelectedValue.ToString();
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.groupText.Text.Trim()))
                {
                    err = "Please enter Group.";
                }
                else
                {
                    group = this.groupText.Text.Trim();
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.stdTime.Text.Trim()))
                {
                    err = "Please enter STD Time.";
                }
                else
                {
                    if (!float.TryParse(this.stdTime.Text.Trim(), out stdtime))
                    {
                        err = "Invalid STD Time";
                    }
                    else
                    {
                        if (stdtime <= 0)
                        {
                            err = "STD Time must be greater than 0.";
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                product = this.productText.Text.Trim();
                if (string.IsNullOrEmpty(this.levelText.Text.Trim()))
                {
                    if (!Int32.TryParse(this.levelText.Text.Trim(), out level))
                    {
                        err = "Invalid level";
                    }
                }
                name = this.nameText.Text.Trim();
                group = this.groupText.Text.Trim();
                serial = this.serialText.Text.Trim();
                remark = this.noteText.Text.Trim();
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "UPDATE [222_Part] SET F5Product = N'" + product + "',"
                        + " F5Level = " + level + ", F5Name = N'" + name + "', AssGroup = N'" + group + "',"
                        + " AssRemark = N'" + remark + "', AssSerial = '" + serial + "',"
                        + " AssSTDTime = " + stdtime.ToString() + ", F5 = 1"
                        + " WHERE PartNo = '" + part + "'";
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    MessageBox.Show("Add Part successfully.");
                }
                catch
                {
                    MessageBox.Show("Cannnot add Part.");
                }
                finally
                {
                    myConnection.Close();
                }
            }
            else
            {
                MessageBox.Show(err);
            }
        }

        private void partNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.partNo.SelectedIndex >= 0)
            {
                string part = this.partNo.SelectedValue.ToString();
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT F5Product, F5Level, F5Name, AssGroup, AssRemark,"
                            + " AssSerial, AssSTDTime FROM [222_Part] WHERE PartNo = '" + part + "'";
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.productText.Text = dataReader["F5Product"].ToString().Trim();
                        this.levelText.Text = dataReader["F5Level"].ToString().Trim();
                        this.nameText.Text = dataReader["F5Name"].ToString().Trim();
                        this.groupText.Text = dataReader["AssGroup"].ToString().Trim();
                        this.serialText.Text = dataReader["AssSerial"].ToString().Trim();
                        this.stdTime.Text = dataReader["AssSTDTime"].ToString().Trim();
                        this.noteText.Text = dataReader["AssRemark"].ToString().Trim();
                    }
                    dataReader.Close();
                    sql_cmd.Dispose();
                }
                catch
                {
                    MessageBox.Show("Cannot get Part Information.");
                }
                finally
                {
                    myConnection.Close();
                }
            }
        }    
    }
}
